//
//  VideosViewController.m
//  Connotation
//
//  Created by LZXuan on 15-4-7.
//  Copyright (c) 2015年 LZXuan. All rights reserved.
//

#import "VideosViewController.h"
#import "VideoModel.h"
#import "VideoCell.h"
#import "MoviePlayerViewController.h"


@interface VideosViewController ()<MoviePlayerViewControllerDataSource,MoviePlayerViewControllerDelegate>

@end

@implementation VideosViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.Nav_title.text=@"视频";
    [self isNetWork];//检测网络状态，解归档
}
-(void)isNetWork{
    BOOL isnet=[LZXHelper reachability];
    if(isnet){
        //第一次下载
        _currentPage = 0;
        self.max_timestamp = @"-1";
        [self loadDataPage:_currentPage count:15];
        
        //再创建刷新
        [self creatRefreshView];
        //点击 加载更多
        [self upPullView];
    }else {
        [self unarchiveMyData:@"video"];
        [self.tableView reloadData];
    }
}

#pragma mark - 重写 父类的方法
//子类重写之后调用子类自己的
- (void)loadDataPage:(NSInteger)page count:(NSInteger)count {
    NSString *url = [NSString stringWithFormat:kContentsUrl,self.category,page,count,self.max_timestamp];
    [_httpRequest downloadRequestWithUrl:url success:^(LZXHttpRequest *request) {
        if (request.downloadData) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.downloadData options:NSJSONReadingMutableContainers error:nil];
            if (_currentPage == 0) {
                [self.dataArr removeAllObjects];
            }
            
            NSArray *arr = dict[@"items"];
            
            for (NSDictionary *videoDict in arr) {
                VideoModel *model = [[VideoModel alloc] init];
                //kvc 赋值
                [model setValuesForKeysWithDictionary:videoDict];
                [self.dataArr addObject:model];
                [model release];
            }
            [self archiveMyData:@"video"];
            //刷新表格
            [self.tableView reloadData];
            _isLoadMore=  NO;
            _isRefreshing = NO;
            [self.headerView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
        }
    } fail:^(NSError *error) {
        NSLog(@"网络异常");
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellName = @"VideoCell";
    VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"VideoCell" owner:self options:nil] lastObject];
    }
    VideoModel *model = self.dataArr[indexPath.row];
    [cell showDataWithModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

//选中cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    VideoModel *mode=[[VideoModel alloc]init];
    mode=self.dataArr[indexPath.row];
    MoviePlayerViewController *move=[[MoviePlayerViewController alloc]initNetworkMoviePlayerViewControllerWithURL:[NSURL URLWithString:mode.vplay_url] movieTitle:mode.wbody];
    
    move.datasource=self;
    move.delegate=self;
    
    [self presentViewController:move animated:YES completion:nil];
    

}
#pragma mark - 代理
- (void)movieFinished:(CGFloat)progress{
    
}
- (NSDictionary *)nextMovieURLAndTitleToTheCurrentMovie{
    return nil;
}
- (NSDictionary *)previousMovieURLAndTitleToTheCurrentMovie{
    return nil;
}
- (BOOL)isHaveNextMovie{
    return NO;
}
- (BOOL)isHavePreviousMovie{
    return NO;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
